package com.flk.oauth.vaildatecode;

import com.flk.oauth.vaildatecode.processor.NeedValidateImageException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.ServletWebRequest;

import javax.xml.bind.ValidationException;
import java.util.concurrent.TimeUnit;

/**
 * @author xxwy
 * on 2018/8/29 0029
 * 基于redis的验证码存取器，避免由于没有session导致无法存取验证码的问题
 */
@Component
public class AppValidateCodeRepository implements ValidateCodeRepository {


    @Autowired
    private RedisTemplate<String, Object> xxredisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void save(ServletWebRequest request, ValidateCode code, ValidateCodeType type) throws ValidationException {
        if (ValidateCodeType.SMS.equals(type)) {
            Object value = stringRedisTemplate.opsForValue().get(buildkey(request, type) + "_num");
            if (value == null) {
                stringRedisTemplate.opsForValue().set(buildkey(request, type) + "_num", "1", 30, TimeUnit.MINUTES);
            } else {
                //点击过次数
                int smsNums = getSmsNums(request, type);
                //增加1
                increment(request,type);
                if (smsNums >= 17) {
                    throw new ValidationException("用户被锁定,30分钟后重试");
                }
                if (smsNums >= 3 && smsNums % 2 == 1) {
                    throw new NeedValidateImageException("需要验证码");
                }
                if (smsNums >= 3 && smsNums % 2 == 0) {
                    xxredisTemplate.opsForValue().set(buildkey(request, type), code, 30, TimeUnit.MINUTES);
                }
            }
        }
        xxredisTemplate.opsForValue().set(buildkey(request, type), code, 30, TimeUnit.MINUTES);
    }

    private void increment(ServletWebRequest request, ValidateCodeType type) {
        stringRedisTemplate.opsForValue().increment(buildkey(request, type) + "_num", 1);
        if (stringRedisTemplate.getExpire(buildkey(request, type) + "_num") == -1) {
            stringRedisTemplate.expire(buildkey(request, type) + "_num", 30, TimeUnit.MINUTES);
        }
    }

    @Override
    public ValidateCode get(ServletWebRequest request, ValidateCodeType type) {
        Object value = xxredisTemplate.opsForValue().get(buildkey(request, type));
        if (value == null) {
            return null;
        }
        return (ValidateCode) value;
    }

    @Override
    public int getSmsNums(ServletWebRequest request, ValidateCodeType type) {
        String s = stringRedisTemplate.opsForValue().get(buildkey(request, type) + "_num");
        if (s != null) {
            return Integer.parseInt(s);
        }
        return 0;
    }

    @Override
    public void remove(ServletWebRequest request, ValidateCodeType type) {
        xxredisTemplate.delete(buildkey(request, type));
    }


    private String buildkey(ServletWebRequest request, ValidateCodeType type) {
        String mobile = request.getParameter("mobile");
        if (StringUtils.isBlank(mobile)) {
            throw new RuntimeException("手机号不能为空");
        }
        return "code:" + type.toString().toLowerCase() + ":" + mobile;
    }
}
